var utils = require("./utils");
var treasure_chest_storage = storages.create("treasure_chest");

function get_treasure_chest_times() {
  return treasure_chest_storage.get("times") || 0;
}
function set_treasure_chest_times(times) {
  treasure_chest_storage.put("times", times);
}
function get_treasure_chest_ad_times() {
  return treasure_chest_storage.get("ad_times") || 0;
}
function set_treasure_chest_ad_times(ad_times) {
  treasure_chest_storage.put("ad_times", ad_times);
}
var treasure_chest_point_map = {
  任务: [278, 1534, 441, 1618],
};

var treasure_chest = {
  init() {
    sleep(5 * 1000);
    if (utils.exists_text("百度") && utils.exists_text("我的")) {
      utils.click_point.apply(null, treasure_chest_point_map["任务"]);
      if (utils.click_text("开宝箱得金币")) {
        var times = get_treasure_chest_times();
        set_treasure_chest_times(times + 1);
        console.log("treasure_chest init:", "宝箱已开", times + 1, "次");
        sleep(2 * 1000);
        var uiText = utils.click_text_starts_with("观看视频再得");
        if (uiText) {
          this.execute(uiText.text());
        } else {
          console.log("treasure_chest init", "没找到【观看视频再得】");
          this.end();
        }
      } else {
        console.log("treasure_chest init", "没找到【开宝箱得金币】");
        this.end();
      }
    } else {
      console.log("treasure_chest init", "没找到【百度】【我的】");
      this.end();
    }
  },
  execute(content) {
    console.log(content);
    var gold = content.match(/(\d+)/g) || 0;
    console.log(gold);
    sleep(2 * 1000);
    console.log("treasure_chest execute:", "2s");
    sleep(random(35, 38) * 1000);
    console.log("treasure_chest execute:", "35s");

    back();

    sleep(2 * 1000);
    this.after();
  },
  after() {
    var ad_times = get_treasure_chest_ad_times();
    set_treasure_chest_ad_times(ad_times + 1);
    console.log("treasure_chest after:", "视频已看", ad_times + 1, "次");
    var uiText = utils.click_text_starts_with("再看一个领取");
    if (uiText) {
      this.execute(uiText.text());
    } else {
      this.end();
    }
  },
  end() {
    sleep(2 * 1000);
    utils.click_text_starts_with("立即收下");
    sleep(2 * 1000);

    if (utils.click_text_starts_with("看广告再得")) {
      this.init();
      return;
    }
    if (!utils.click_text("百度")) {
      console.log("treasure_chest end:", "没找到【百度】");
    }
    console.log("treasure_chest end:");
    this.next();
  },
  next() {
    read_article.init();
  },
};

var read_article_storage = storages.create("read_article");

function get_read_article_times() {
  return read_article_storage.get("times") || 0;
}
function set_read_article_times(times) {
  read_article_storage.put("times", times);
}

var read_article = {
  seconds: 0.5 * 60,
  init() {
    sleep(5 * 1000);
    if (utils.click_text("推荐")) {
      sleep(2 * 1000);
      this.execute(this.seconds);
    } else {
      console.log("read_article init", "没找到【推荐】");
      this.end();
    }
  },
  execute(seconds) {
    var start_time = Date.now();
    while (true) {
      swipe(
        device.width / 2,
        device.height / 2,
        device.width / 2,
        device.height / 2 - 800,
        1000
      );
      var interval = random(1, 5);
      sleep(interval * 1000);
      var end_time = Date.now();
      console.log(
        "read_article execute:",
        "文章已看",
        ((end_time - start_time) / 1000).toFixed(2),
        "s",
        ((end_time - start_time) / 1000 / 60).toFixed(2),
        "m"
      );
      if (end_time - start_time > seconds * 1000) {
        break;
      }
    }
    this.after();
  },
  after() {
    this.end();
  },
  end() {
    sleep(2 * 1000);
    utils.click_text("百度");
    console.log("read_article end:");
    watch_video.init();
  },
};

var watch_video_storage = storages.create("watch_video");

var watch_video_point_map = {
  视频: [147, 1534, 278, 1618],
};

function get_watch_video_times() {
  return watch_video_storage.get("times") || 0;
}
function set_watch_video_times(times) {
  watch_video_storage.put("times", times);
}

var watch_video = {
  seconds: 0.5 * 60,
  init() {
    sleep(5 * 1000);
    if (utils.exists_text("视频")) {
      utils.click_point.apply(null, watch_video_point_map["视频"]);
      sleep(2 * 1000);
      this.execute(this.seconds);
    } else {
      console.log("watch_video init", "没找到【视频】");
      this.end();
    }
  },
  execute(seconds) {
    var start_time = Date.now();
    while (true) {
      swipe(
        device.width / 2,
        device.height / 1.2,
        device.width / 2,
        device.height / 1.2 - 1500,
        1000
      );
      var interval = random(5, 15);
      sleep(interval * 1000);
      var end_time = Date.now();
      console.log(
        "watch_video execute:",
        "视频已看",
        ((end_time - start_time) / 1000).toFixed(2),
        "s",
        ((end_time - start_time) / 1000 / 60).toFixed(2),
        "m"
      );
      if (end_time - start_time > seconds * 1000) {
        break;
      }
    }
    this.after();
  },
  after() {
    this.end();
  },
  end() {
    sleep(2 * 1000);
    if (!utils.click_text("百度")) {
      console.log("watch_video end:", "没找到【百度】");
    }
    console.log("watch_video end:");
    treasure_chest.init();
  },
};

function main() {
  sleep(2 * 1000);
  launchApp("百度极速版");
  sleep(4 * 1000);
  treasure_chest.init();
}
main();
